﻿import { Component, Injector, ViewChild , Input} from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { RequirementPackageDto,ImportAllotDto } from '@shared/service-proxies/service-proxies';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { Tab } from './tab.model';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { RequirementImportItemEditComponent } from './requirement-import-item_edit.component'
import { RequirementPackageServiceProxy, RequirementItemDto,StandardDetailType ,UsingFormServiceProxy} from '@shared/service-proxies/service-proxies';
import { FileDownloadService } from '@shared/utils/file-download.service';

@Component({
    selector: 'fetchImportAbnormalTabComponent',
    templateUrl: './fetch-import-abnormal_tab.component.html',
    styleUrls: ['./fetch-import-abnormal_tab.component.less'],
    animations: [appModuleAnimation()]
})
export class FetchImportAbnormalTabComponent extends AppComponentBase{
    @ViewChild('requirementImportItemEditComponent') requirementImportItemEditComponent: RequirementImportItemEditComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    //@ViewChildren('dataTable2') dataTable2: QueryList<Table>;
    //@ViewChildren('paginator2') paginator2: QueryList<Paginator>;
    //primengTableHelpers: Array<PrimengTableHelper>;
    tabs: Array<Tab>;
    tabObject:{
        filterText:""
    };
    loading = false;
    caches: any = null;
    logs: any = '';
    filterText = '';

    active =false;
    hidden=true;
    title:"hello";
    @Input() requirementPackage: RequirementPackageDto;
    

    constructor(
        injector: Injector,
        private requirementPackageServiceProxy: RequirementPackageServiceProxy,
        private _usingFormServiceProxy: UsingFormServiceProxy,
        private _fileDownloadService :FileDownloadService) {
        super(injector);
        this.active = false;
        //this.tabs = new Array<Tab>();
        //this.primengTableHelpers = new Array<PrimengTableHelper>();
    }
    exportAbnormal(): void {
        if(this.primengTableHelper.records.length>0){
            this._usingFormServiceProxy.getImportAllotDtoExcel(this.primengTableHelper.records)
                .subscribe(result => {
                    this._fileDownloadService.downloadTempFile(result);
                });
        }
    }
    addTab(requirementItemDto: RequirementItemDto): void {
        this.active = true;
        //*ngFor="let tab of tabs"
        /*let tab: Tab = new Tab(requirementPackageDto.displayName);
        tab.index = this.tabs.length;
        tab.requirementPackageId = requirementPackageDto.id;
        tab.active = true;
        this.clearTab();
        this.tabs.push(tab);*/
        
        //this.primengTableHelpers.push(new PrimengTableHelper());
    }
    clearTab(): void {
        /*
        while (this.tabs.length > 0) {
            let obj = this.tabs.shift();
            obj.active = false;
        }*/
    }
    createRequirementItem(event?: LazyLoadEvent):void{
        //this.requirementItemEditComponent.show();
    }
        /**
    * 弹出添加需求详细页面
    * 弹出编辑需求详细页面
    * @param requirementItemDto 
    */
    createOrEditRequirementItem(requirementItemDto : RequirementItemDto):void{
        //this.requirementItemEditComponent.show(this.requirementPackage, requirementItemDto);
    }

    createOrEditRequirementPackage(record:ImportAllotDto): void {
        this.requirementImportItemEditComponent.show(record);
    }
    deleteRequirementPackage(requirementItemDto :RequirementItemDto): void {
        this.message.confirm(
            '需求包将被删除',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this.requirementPackageServiceProxy.deleteItemsOfPackage(requirementItemDto.id)
                    .pipe(finalize(() => { this.getRequirementItems();}))
                    .subscribe(() => {
                        this.notify.info(this.l('DeletedSuccessfully'));
                    });
                }
            }
        );
    }
    setRequirementPackage(_pRequirementPackage: RequirementPackageDto): void {
        this.requirementPackage = _pRequirementPackage;
    }
    getRequirements( tab:Tab,event?: LazyLoadEvent) {
        this.primengTableHelper.showLoadingIndicator();
        /*let that=this;
        this._mainListServiceProxy.getItemsOfPackage(tab.requirementPackageId)
        .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            that.primengTableHelpers[tab.index].records = result;
            that.primengTableHelpers[tab.index].hideLoadingIndicator();
        });*/
    }   
    getStandarType(standardType:string):string{
        return StandardDetailType[standardType];
    } 
    /**
    * 检索需求详细
    * @param requirementItemDto 
    */
    getRequirementItems(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        /*this.primengTableHelper.records = [];
        this.primengTableHelper.showLoadingIndicator();
        this._testDataServiceProxy.getAllDatas(
            this.filterText,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event))
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });*/
    }
    /**
     * 删除需求详细
     * @param requirementItemDto 
     */
    deleteRequirementItems(requirementItemDto:RequirementItemDto){
        this.message.confirm(
            '删除该需求？',
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this.requirementPackageServiceProxy.deleteItemsOfPackage(requirementItemDto.id)
                    .pipe(finalize(() => { this.getRequirementItems();}))
                    .subscribe(() => {
                        this.notify.info(this.l('DeletedSuccessfully'));
                    });
                }
            }
        );
    }
    reloadPage():void {
        var ddd ='';
    }

    onResize(event): void {
        
    }

    ngOnInit(): void {
        const self = this;
    }
}
